package work.gac.unbind.v2;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Gong.Yang
 */
@Slf4j
public class DatalakUtil {

    /**
     * 通过手机号获取OpenID
     */
    public static String getOpenId(String mobile, String fromSocialCode) {
        List<MediaAccount> mediaAccounts = DatalakUtil.getByMobile(mobile);
//        log.info("mediaAcc->{}", mediaAccounts);
        for (MediaAccount mediaAccount : mediaAccounts) {
            if (mediaAccount.getFromSocialCode().equals(fromSocialCode)) {
                //校验
                if (mediaAccount.getBindStatus() != 1) {
                    log.info("【解绑】非绑定会员，mediaAccount->{}", JSONUtil.toJsonStr(mediaAccount));
                    return null;
                }
                return mediaAccount.getAccountNo();
            }
        }
        log.info("【解绑】用户没有fromSocialCode->{},mobile->{}", fromSocialCode, mobile);
        return null;
    }

    /**
     * DL解绑
     */
    public static void unbind(String mobile, String openId) {
        MemberUtil.unbindByMobileOpenid(mobile, openId, Setting.Fix.manageBaseUrl, Setting.Fix.manageToken);
//        if (StrUtil.hasBlank(openId, mobile)) {
//            return;
//        }
//        //解绑body
//        JSONObject unbindBody = new JSONObject();
//        unbindBody.set("channel", Setting.miniChannelCode);
//        unbindBody.set("brand_code", Setting.brandCode);
//        unbindBody.set("program_code", Setting.programCode);
//        unbindBody.set("type", "0");// 1:绑定；0:解绑
//
//        //解绑params
//        JSONObject params = new JSONObject();
//        params.set("mobile", mobile);
//        params.set("open_id", openId);
//        unbindBody.set("params", params);
//
//        //解绑url
//        String unbindUrl = String.format(Setting.URL.DATA_LAKE_UNBIND, Setting.url);
//        Map<String, String> unbindUrlRequestHeader = new HashMap<>(16);
//        unbindUrlRequestHeader.put("ruid", JSONUtil.toJsonStr(new JSONObject().set("uuid", StrUtil.uuid())));
//        unbindUrlRequestHeader.put("Content-Type", "application/json; charset=utf-8");
//        unbindUrlRequestHeader.put("Authorization", Setting.Fix.datalakeToken);
//        String body = HttpRequest.post(unbindUrl).addHeaders(unbindUrlRequestHeader)
//                .timeout(10000).body(JSONUtil.toJsonStr(unbindBody)).execute().body();
//
    }

    /**
     * 通过手机号获取MediaAccount
     */
    public static List<MediaAccount> getByMobile(String mobile) {
//        String url = String.format(Setting.URL.DATA_LAKE_QUERY_MEMBER_INFO, Setting.url);
//        JSONObject requestBody = new JSONObject();
//        requestBody.set("Brand_code", Setting.brandCode);
//        requestBody.set("Program_code", Setting.programCode);
//        requestBody.set("queryType", "mobile");
//        requestBody.set("Value", mobile);
//
//        Map<String, String> requestHeader = new HashMap<>();
//        requestHeader.put("ruid", JSONUtil.toJsonStr(new JSONObject().set("uuid", StrUtil.uuid())));
//        requestHeader.put("Content-Type", "application/json; charset=utf-8");
//        requestHeader.put("Authorization", Setting.Fix.datalakeToken);
//
//        String body = HttpRequest.post(url).addHeaders(requestHeader).timeout(10000).body(JSONUtil.toJsonStr(requestBody)).execute().body();
//
//        JSONObject response = JSONUtil.parseObj(body);
        JSONObject response = MemberUtil.findDLCustomerInfo(mobile, Setting.Fix.manageBaseUrl, Setting.Fix.manageToken);
        if (StrUtil.isNotBlank(response.get("code", String.class))) {
            log.info("异常:response->{},mobile->{}", response, mobile);
            return null;
        }
//        if (response.get("code", Integer.class) != 0) {
//            log.info("异常code mobile->{}, message:{}", mobile, response.get("msg"));
//            return null;
//        }
        JSONArray mediaAccounts = response.getJSONArray("media_account");
        List<MediaAccount> list = new ArrayList<>();
        for (int i = 0; i < mediaAccounts.size(); i++) {
            MediaAccount item = mediaAccounts.get(i, MediaAccount.class);
            list.add(item);
        }
        return list;
    }
}
